package de.edu.tum.agora;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.Iterator;

import org.osgi.framework.BundleContext;

import de.edu.tum.agora.user.Administrator;
import de.edu.tum.agora.user.Developer;
import de.edu.tum.agora.user.EndUser;
import de.edu.tum.agora.user.User;
import de.edu.tum.service.AService;

public class AgoraImpl extends AService {
	private String userinput;
	private int userID;
	private HashMap<String, User> users;
	private User user;
	private BundleContext context;

	public AgoraImpl(BundleContext context) {
		super(context);
		this.context = context;
		user = null;
		userID = 0;
		users = new HashMap<>();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void run() {

		while (user == null) {
			System.out.println("1. Create User");
			System.out.println("2. Log In");
			System.out.println("3. Get all users");
			userinput = getInput();
			switch (Integer.parseInt(userinput)) {
			case 1:
				createUser();
				break;
			case 2:
				user = login();
				if (user != null) {
					System.out.println("Login succesful, hello "
							+ user.getName());
				} else {
					System.out.println("Wrong user name or password");
				}
				break;
			case 3:
				getUsers("/files/users.txt");
				break;
			default:
				System.out.println("Make correct choice");
				break;
			}
		}

		if (user instanceof Developer) {
			// TODO menu options for Developer
		} else if (user instanceof Administrator) {
			// TODO menu options for Administrator
		} else {
			// TODO menu options for EndUser
		}
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void suspend() {
		// TODO Auto-generated method stub

	}

	public void install() {
		// TODO Install
	}

	public void uninstall() {
		// TODO UnInstall
	}

	/***
	 * Create new user
	 * 
	 * @return {@link User}
	 */
	private void createUser() {
		// TODO User creation
		String name;
		String email;
		String username;
		String password;
		int type;
		System.out.println("Enter your name");
		name = getInput();
		System.out.println("Enter your username");
		username = getInput();
		System.out.println("Enter your password");
		password = getInput();
		System.out.println("Enter your email");
		email = getInput();
		System.out.println("Enter your type");
		type = Integer.parseInt(getInput());
		double initRating = 0.0;
		User user = null;
		switch (type) {
		case 0:
			user = new Developer(userID++, name, username, password, email,
					initRating);
			break;
		case 1:
			user = new Administrator(userID++, name, username, password, email,
					initRating);
			break;
		case 2:
			user = new EndUser(userID++, name, username, password, email,
					initRating);
			break;
		default:
			break;
		}
		System.out
				.println("User \"" + username + "\" was successfully created");
		users.put(username, user);
	}

	/***
	 * Log in to system.
	 * 
	 * @return {@link User} if successful, null if not.
	 */
	private User login() {
		// TODO Log in
		String username;
		String password;
		System.out.println("Please enter user name");
		username = getInput();
		System.out.println("Please enter password");
		password = getInput();
		if (users.containsKey(username)) {
			if (users.get(username).getPassword().equals(password)) {
				return users.get(username);
			}
		}
		return null;
	}

	/***
	 * Gets input from the user
	 * 
	 * @return String value of an input
	 */
	private String getInput() {
		Scanner in = new Scanner(System.in);
		String input = in.nextLine();
		return input;
	}

	/***
	 * Gets all users from file {@value filePath}, and outputs usernames of all
	 * users
	 * 
	 * @param filePath
	 *            location where users file is located
	 */
	private void getUsers(String filePath) {
		User user = null;
		URL entryUrl = context.getBundle().getEntry(filePath);
		try {
			BufferedReader bReader = new BufferedReader(new InputStreamReader(
					entryUrl.openStream()));
			String line;
			while ((line = bReader.readLine()) != null) {
				String datavalue[] = line.split("\t");
				int id = Integer.parseInt(datavalue[0]);
				String username = datavalue[1];
				String password = datavalue[2];
				String name = datavalue[3];
				String email = datavalue[4];
				double rating = Double.parseDouble(datavalue[5]);
				int type = Integer.parseInt(datavalue[6]);
				switch (type) {
				case 1:
					user = new Developer(id, name, username, password, email,
							rating);
					break;
				case 2:
					user = new Administrator(id, name, username, password,
							email, rating);
				default:
					user = new EndUser(id, name, username, password, email,
							rating);
					break;
				}
				if (user != null) {
					users.put(username, user);
				}
			}
			for (String string : users.keySet()) {
				System.out.println(string);
			}
			bReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void prepareMigration() {
		// TODO Auto-generated method stub

	}

	@Override
	public void prepareReplication() {
		// TODO Auto-generated method stub

	}

}
